package performance;

import struct.enums.CtxValidator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PerformanceExperiment {

    static String RESOURCES_DIR = System.getProperty("user.dir") + "/demo/s_service/resources/";
//    static String RESOURCES_DIR = "Resources/resources/";

    public static void main(String[] args) throws Exception {
        List<CtxValidator> ctxValidatorList = List.of(
               CtxValidator.INFUSE,
                CtxValidator.PCC_IMD,
                CtxValidator.ECC_GEAS,
                CtxValidator.ECC_IMD
        );

        HashMap<CtxValidator, String> idMap = new HashMap<>(){{
            put(CtxValidator.INFUSE, "SEPAL_INFuse");
            put(CtxValidator.PCC_IMD, "SEPAL_PCC");
            put(CtxValidator.ECC_GEAS, "SEPAL_GEAS");
            put(CtxValidator.ECC_IMD, "ECC");
        }};

        Map<CtxValidator, Long> checkingTimeMap = new HashMap<>();
        System.out.println("Starts running performance experiment......");

        System.out.println("Table 1: Execution Times\n");
        System.out.println("----------------------------------\n");
        System.out.printf("%-15s %-15s\n%n", "Technique", "Time (ms)");
        System.out.println("----------------------------------\n");

        for (CtxValidator ctxValidator : ctxValidatorList) {
            Thread.sleep(1000); // for fair comparison
            PTCtxServer ptCtxServer = new PTCtxServer(
                    RESOURCES_DIR + "consistency_patterns_48.xml",
                    RESOURCES_DIR + "consistency_rules_48.xml",
                    RESOURCES_DIR + "bfuncs.java",
                    ctxValidator,
                    RESOURCES_DIR + "data.txt"
            );
            ptCtxServer.init();
            Long startTime = System.currentTimeMillis();
            ptCtxServer.runCheck();
            Long endTime = System.currentTimeMillis();

            System.out.printf("%-15s %-15s\n%n", idMap.get(ctxValidator), endTime - startTime);
            checkingTimeMap.put(ctxValidator, endTime - startTime);
        }

        System.out.println("\n\n");
        System.out.println("Table 2: Efficiency Improvements\n");
        System.out.println("-------------------------------------------------------------------\n");
        System.out.printf("%-15s %-15s %-15s\n%n", "Technique A (Faster Technique)", "Technique B (Slower Technique)", "Efficiency Improvement (X)");
        System.out.println("-------------------------------------------------------------------\n");
        CtxValidator[] methods = checkingTimeMap.keySet().toArray(new CtxValidator[0]); // 将键转换为数组
        for (CtxValidator ctxValidator : methods){
            CtxValidator ecc = CtxValidator.ECC_IMD;
            if (ctxValidator.equals(ecc)) continue;
            double improvement = ((double) checkingTimeMap.get(ecc) / checkingTimeMap.get(ctxValidator) - 1);
            System.out.printf("%-15s %-15s %-15.2f\n%n", idMap.get(ctxValidator), idMap.get(ecc), improvement);
        }
        System.out.println("-------------------------------------------------------------------");
    }
}